<template>
  <div class="app-container">
    <!-- 移动端顶部栏 -->
    <div class="mobile-header" v-if="isMobile">
      <div class="mobile-menu-button" @click="toggleSidebar">
        <div class="hamburger" :class="{ open: isSidebarOpen }">
          <span></span>
          <span></span>
          <span></span>
        </div>
      </div>
      <div class="mobile-logo">
        <img src="@/assets/logo.png" alt="ZBOX" class="logo-image" />
      </div>
    </div>

    <!-- 侧边栏 - 在移动端时变为抽屉 -->
    <div class="sidebar-container" :class="{ mobile: isMobile, open: isSidebarOpen || !isMobile }">
      <Sidebar />
    </div>

    <!-- 遮罩层 - 点击时关闭抽屉 -->
    <transition name="fade">
      <div class="overlay" v-if="isMobile && isSidebarOpen" @click="closeSidebar"></div>
    </transition>

    <Content />
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
import Sidebar from './components/Sidebar.vue'
import Content from './components/Content.vue'

// 响应式状态
const isMobile = ref(false)
const isSidebarOpen = ref(false)

// 检查是否为移动设备
const checkMobile = () => {
  isMobile.value = window.innerWidth <= 768
}

// 切换侧边栏
const toggleSidebar = () => {
  isSidebarOpen.value = !isSidebarOpen.value
}

// 关闭侧边栏
const closeSidebar = () => {
  isSidebarOpen.value = false
}

// 监听窗口大小变化
onMounted(() => {
  checkMobile()
  window.addEventListener('resize', checkMobile)
})

onUnmounted(() => {
  window.removeEventListener('resize', checkMobile)
})

// 侧边栏打开关闭时，处理body滚动
watch(isSidebarOpen, (isOpen) => {
  if (isMobile.value) {
    if (isOpen) {
      document.body.classList.add('body-scroll-lock')
    } else {
      document.body.classList.remove('body-scroll-lock')
    }
  }
})
</script>

<style>
/* 全局样式 - 当抽屉打开时禁止滚动 */
.body-scroll-lock {
  overflow: hidden !important;
  height: 100% !important;
}

/* 过渡动画 */
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.3s ease;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

<style scoped>
.app-container {
  display: flex;
  height: 100%;
  padding: 20px;
  gap: 20px;
  position: relative;
}

/* 移动端顶部栏样式 */
.mobile-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
  background-color: #ffffff;
  display: flex;
  align-items: center;
  padding: 0 20px;
  z-index: 1000;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

/* 移动端菜单按钮样式 */
.mobile-menu-button {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

/* 移动端Logo样式 */
.mobile-logo {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-right: 20px;
}

.mobile-logo .logo-image {
  height: 30px;
  max-width: 100px;
  object-fit: contain;
}

/* 汉堡菜单样式 */
.hamburger {
  width: 20px;
  height: 16px;
  position: relative;
  transition: all 0.3s;
}

.hamburger span {
  display: block;
  position: absolute;
  height: 2px;
  width: 100%;
  background: #333;
  border-radius: 2px;
  opacity: 1;
  left: 0;
  transform: rotate(0deg);
  transition: all 0.25s ease-in-out;
}

.hamburger span:nth-child(1) {
  top: 0px;
}

.hamburger span:nth-child(2) {
  top: 7px;
}

.hamburger span:nth-child(3) {
  top: 14px;
}

.hamburger.open span:nth-child(1) {
  top: 7px;
  transform: rotate(135deg);
}

.hamburger.open span:nth-child(2) {
  opacity: 0;
  left: -60px;
}

.hamburger.open span:nth-child(3) {
  top: 7px;
  transform: rotate(-135deg);
}

/* 侧边栏容器 */
.sidebar-container {
  transition: transform 0.3s ease;
}

/* 移动端侧边栏样式 */
.sidebar-container.mobile {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  z-index: 999;
  transform: translateX(-100%);
  box-shadow: none;
  padding: 0;
}

.sidebar-container.mobile.open {
  transform: translateX(0);
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.2);
}

/* 遮罩层 */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 998;
}

@media (max-width: 768px) {
  .app-container {
    padding: 0;
    padding-top: 70px;
    height: auto;
    min-height: 100%;
    display: block; /* 不使用flex布局 */
  }

  /* 当侧边栏打开时，禁止页面滚动 */
  :root {
    overflow: hidden;
    height: 100%;
  }
}
</style>
